package com.hyc.algorithm.Array;

/**
 * @projectName: DataStructure
 * @package: com.hyc.algorithm.Array
 * @className: ArraySum
 * @author: 冷环渊 doomwatcher
 * @description: TODO
 *  数组的遍历与求和
 * @date: 2022/2/27 19:37
 * @version: 1.0
 */
public class ArraySum {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        showArray(arr);
        int arraySum = arraySum(arr);
        System.out.println("\n" + "数组遍历相加之后=>" + arraySum);
    }

    //数组求和
    public static int arraySum(int[] arr) {
        //    判断非空
        if (arr.length == 0) {
            return -1;
        }
        int res = 0;
        //遍历数组，前后相加
        for (int i = 0; i < arr.length; i++) {
            res += arr[i];
        }
        return res;
    }

    //数组遍历
    public static void showArray(int[] arr) {
        //遍历数组，前后相加
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + ",");
        }
    }

}
